package com.hgd.security.evp.auth.authentication;

import com.alibaba.fastjson.JSONObject;
import com.hgd.security.evp.common.util.ObjectResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义用户登录成功的响应
 * @author By 尚
 * @date 2019/3/30 16:55
 */
@Component
@Slf4j
public class HgdAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
        log.info("登录成功");
        response.setContentType("application/json;charset=UTF-8");

        ObjectResultResponse<Void> result=new ObjectResultResponse<>();
        result.setMessage("登录成功");

        response.getWriter().write(JSONObject.toJSONString(result));
    }
}
